<?php
/**
 * UploadService.php class file.
 * @author wudg <544038230@qq.com>
 * @date 2020/11/25- 1:55 下午
 */

namespace common\services;


use AliCloud\OSS\Core\OssException;
use AliCloud\OSS\OssClient;
use common\exception\ServicesException;
use common\helpers\ImagesHelper;
use common\models\com\ComImages;
use yii\base\UserException;
use yii\helpers\FileHelper;
use yii\web\UploadedFile;
use yii\base\Exception;
use common\exception\CopyException;
use common\helpers\JobHelper;
use common\constants\CopyConst;
use common\helpers\StringHelper;
use common\models\com\ComSpider;
use common\models\pdd\PddCopyProductsRefound;
use common\dao\ComSpiderDao;
use common\jobs\dao\PddCopyProductsDao;
use common\dao\PddCopyProductsRefoundDao;
use common\drivers\SpiderProductDriverGY;
use Yii;

class CopyrefoundService
{
    public function getcrawler()
    {
        $nowtime = time() - 300;
        $sql = 'select id,request_id,mall_id,copyid,times from pdd_copy_products_refound where times < 3 and update_time <' . $nowtime . ' order by update_time asc limit 1000';
        $command = Yii::$app->db->createCommand($sql);
        $res = $command->queryAll();//获取本时间段需要上下架的商品
        //调光云接口
        $driverFilter = (new SpiderProductDriverGY())->getSpiderDriver('ApiSpider');
        if ($res) {
            foreach ($res as $k => $v) {
                try {
                    if ($v['request_id']) {
                        //
                        $productRefound = (new PddCopyProductsRefoundDao())->getInfoByPk($v['id']);
                        if ($v['times'] == 2) {
                            $productRefound->delete();
                        } else {
                            $productRefound->times = $productRefound->times + 1;
                            $productRefound->update_time = time();
                            $productRefound->save();
                        }
                        $content = $driverFilter->itemDataCallBack($v['request_id']);
                        if ($content) {
                            $contents = json_decode($content, true);
                            if (isset($contents['list']['goodsItem']) && !empty($contents['list']['goodsItem'])) {
                                foreach ($contents['list']['goodsItem'] as $key => $val) {
                                    $logInfo = (new PddCopyProductsDao())->getInfoByRequestId($v['request_id'], $val['goods_id']);
                                    if ($logInfo->status == 0 || $logInfo->status == 1 || $logInfo->status > 0) {
                                        //5分钟还是采集中或者是采集结束的重新入队列执行上货操作
                                        if (!Yii::$app->db->isActive) {
                                            Yii::$app->db->open();
                                        }
                                        $logInfo->status = CopyConst::COPY_SPIDER_YES;
                                        if ($logInfo) {
                                            $logInfo->save();
                                        }
                                        $spiderInfo = (new ComSpiderDao())->getByPlatAndNumId($val['platform'], $val['goods_id']);
                                        if (!$spiderInfo) {
                                            $spiderInfo = new ComSpider();
                                        }
                                        $spiderInfo->platform = $val['platform'];
                                        $spiderInfo->plat_id = $val['goods_id'];
                                        $spiderInfo->lasttime_at = time();

                                        $spiderInfo->extend_data = StringHelper::jsonEncode($driverFilter->formatContent($val));

                                        $spiderInfo->note = StringHelper::jsonEncode($driverFilter->getErrorInfo());
                                        $spiderInfo->save();
                                        Yii::$app->db->close();
                                        if ($logInfo) {
                                            switch ($logInfo->callback) {
                                                //直接上货
                                                case 'copyToPdd':
                                                    $jobArgs = [
                                                        'log_id' => $logInfo->id, // 日志表主键
                                                        'batch_id' => $logInfo->batch_no,// 批次号id
                                                        'spider_id' => $spiderInfo->id, //com_spider 表主键
                                                        'filter_id' => $logInfo->tpl_id, //pdd_filter_tpl 表主键
                                                        'uid' => $logInfo->uid,// uid
                                                        'mall_id' => $logInfo->mall_id //店铺id
                                                    ];
                                                    JobHelper::toCopyrefound($jobArgs);
                                                    //删除日志表信息
                                                    //PddCopyProductsRefound::findOne($v['id'])->delete();
                                                    //$this->writeSpiderLog('第十四步骤:投入上货队列成功');
                                                    break;
                                                //搬家到云库
                                                case 'copyToCloud':
                                                    $jobArgs = [
                                                        'log_id' => $logInfo->id, // 日志表主键
                                                        'batch_id' => $logInfo->batch_no,// 批次号id
                                                        'spider_id' => $spiderInfo->id, //com_spider 表主键
                                                        'filter_id' => $logInfo->tpl_id, //pdd_filter_tpl 表主键
                                                        'uid' => $logInfo->uid,// uid
                                                        'store_id' => $logInfo->store_id,// 所属仓库
                                                        'mall_id' => $logInfo->mall_id //店铺id
                                                    ];
                                                    JobHelper::toCloudrefound($jobArgs);
                                                    break;
                                                default:
                                                    throw new CopyException('暂无设置其他任务');
                                            }

                                        }
                                    }
                                }
                            }
                        }
                    }
                }catch (Exception $e){
                    continue;
                }
            }
        }


    }
}
